package pro.seac.twitter.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class UserAccount extends BaseEntity {

	@Column(length = 512, nullable = false)
	private String username;

	@Column(length = 512, nullable = false)
	private String email;

	@Column(length = 512, nullable = false)
	private String name;
	// TODO Haslo trzymane palint text!! zmien to
	@Column(length = 128, nullable = false)
	private String password;

	@Column(length = 1024, nullable = true)
	private String about;

	// sledzeni
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "following", joinColumns = { /* sledzacy */@JoinColumn(name = "follower", referencedColumnName = "id") }, /* sledzony */inverseJoinColumns = { @JoinColumn(name = "followed", referencedColumnName = "id") })
	private Set<UserAccount> followed;

	// sledzacy
	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "followed")
	private Set<UserAccount> followers;

	public void addFollowingUser(UserAccount user) {
		if (followed == null) followed = new HashSet<UserAccount>();
		followed.add(user);
		if (user.followers == null) user.followers = new HashSet<UserAccount>();
		user.followers.add(this);
	}

	public boolean isAlreadyFollowingUser(Integer id) {
		if (followed == null) return false;
		for (UserAccount f : followed) {
			if (id.equals(f.getId())) { return true; }
		}
		return false;
	}

	public void removeFollowingUser(UserAccount user) {
		if (followed == null) return;
		followed.remove(user);
		if (user.followers == null) return;
		user.followers.remove(this);
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAbout() {
		return about;
	}

	public void setAbout(String about) {
		this.about = about;
	}

	public Set<UserAccount> getFollowed() {
		return followed;
	}

	public void setFollowed(Set<UserAccount> followed) {
		this.followed = followed;
	}

	public Set<UserAccount> getFollowers() {
		return followers;
	}

	public void setFollowers(Set<UserAccount> followers) {
		this.followers = followers;
	}

}
